<?php
namespace Home\Controller;
use Think\Controller;
class RegController extends Controller {    //注册控制器
    public function index(){

		$question=D('verify')->getOneVerify();

		$this->assign('question',$question);  //分配

       	$this->display();   //引模板(index.html)
   }

    public function checkData()
    {
    	$password = I('post.password');
    	$repassword = I('post.repassword');

        //判断密码是否一致
    	if($password!=$repassword){
    		$arr = array(
    			'error'=>1,
    			'info'=>'两次密码不一致'
    			);
    		echo json_encode($arr);
    		exit;
    	}

    	$verify = I('post.verify');

        // 判断人类验证是否正确
    	if($verify != $_SESSION['answer']){
    		$arr = array(
    			'error'=>1,
    			'info'=>'人类验证不通过'
    			);
    		echo json_encode($arr);
    		exit;
    	}

        // 邮箱检测
    	if(D('user')->checkEmail(I('post.email'))){
    		$arr = array(
    			'error'=>1,
    			'info'=>'邮箱已存在'
    			);
    		echo json_encode($arr);
    		exit;
    	}

        //检测昵称
    	if(D('user')->checkNickname(I('post.nickname'))){
    		$arr = array(
    			'error'=>1,
    			'info'=>'昵称已存在'
    			);
    		echo json_encode($arr);
    		exit;
    	}

    	$data = I('post.');

    	$data['password']= md5($password);

    	$data['create_time']=time();

        // 添加新用户
    	if(M('user')->add($data)){
    		$arr = array(
    			'error'=>0,
    			'info'=>'注册成功'
    			);
    		echo json_encode($arr);
    		exit;
    	}else{
    		$arr = array(
    			'error'=>1,
    			'info'=>'注册失败'
    			);
    		echo json_encode($arr);
    		exit;
    	}


    }
}



?>